/*
 * @(#) WorkerAction.java 2011-9-7
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.system.presentation.action;

import java.util.ArrayList;
import java.util.List;

import cn.strong.gweva.common.business.entity.ComboBoxEntity;
import cn.strong.gweva.common.business.entity.PagerEntity;
import cn.strong.gweva.common.business.service.ComboBoxService;
import cn.strong.gweva.common.presentation.action.AjaxBaseAction;
import cn.strong.gweva.system.business.entity.WorkerEntity;
import cn.strong.gweva.system.business.entity.WorkerSearchEntity;
import cn.strong.gweva.system.business.service.WorkerService;


/**
 * 工作人员管理Action.
 * 
 * @author jincong
 * @version 1.0
 * @since 2011-9-7
 */
public class WorkerAction extends AjaxBaseAction
{

	/** serialVersionUID */
	private static final long serialVersionUID = 1999448294212812899L;

	/** 工作人员 */
	private WorkerEntity worker;

	/** 工作人员service */
	private WorkerService workerService;

	/** 下拉框service */
	private ComboBoxService comboBoxService;

	/** 工作人员检索结果（分页） */
	private PagerEntity pagerEntity;

	/** 工作人员检索条件 */
	private WorkerSearchEntity workerSearchEntity;

	/**
	 * 新增工作人员初始化.
	 * 
	 * @return
	 */
	public String add()
	{
		return ADD;
	}

	/**
	 * 新增工作人员.
	 * 
	 * @return
	 */
	public String insert()
	{

		try
		{

			this.worker.setUpdateUserId(this.getLoginUser().getUserId());
			this.workerService.insert(this.worker);
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return this.list();
	}

	/**
	 * 修改工作人员初始化.
	 * 
	 * @return
	 */
	public String modify()
	{
		try
		{

			this.worker = this.workerService.searchWorkerById(this.worker);
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return MODIFY;
	}

	/**
	 * 工作人员资料
	 * @return
	 */
	public String detail()
	{
		try
		{

			this.worker = this.workerService.searchWorkerById(this.worker);
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return DETAIL;
	}

	/**
	 * 工作人员资料（登录用户）
	 * @return
	 */
	public String detailMe()
	{
		try
		{
			this.setWorker(new WorkerEntity());
			this.getWorker().setWorkerCode(this.getLoginUser().getWorkerCode());
			this.worker = this.workerService.searchWorkerById(this.worker);
			if (this.worker == null)
			{
				this.setIsSuccess("工作资料不存在！");
			}
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return "detailMe";
	}

	/**
	 * 修改工作人员.
	 * 
	 * @return
	 */
	public String update()
	{
		try
		{

			this.worker.setUpdateUserId(this.getLoginUser().getUserId());
			this.workerService.update(this.worker);
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return this.list();
	}

	/**
	 * 删除工作人员.
	 * 
	 * @return
	 */
	public String delete()
	{
		try
		{

			this.worker.setUpdateUserId(this.getLoginUser().getUserId());
			this.workerService.delete(this.worker);
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return this.list();
	}

	/**
	 * 工作人员列表.
	 * 
	 * @return
	 */
	public String list()
	{

		try
		{
			this.pagerEntity = this.workerService.searchWorkerPage(this.pagerEntity,
					this.workerSearchEntity);
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return LIST;
	}

	/**
	 * 工作人员列表.
	 * 
	 * @return
	 */
	public String selectList()
	{

		try
		{
			this.pagerEntity = this.workerService.searchWorkerPage(this.pagerEntity,
					this.workerSearchEntity);
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return "select";
	}
	
	/**
	 * 校验工作人员编号存在性
	 */
	public String checkWorkerCodeExist()
	{
		try
		{
			writeJson(workerService.checkWorkerCodeExist(worker.getWorkerCode()));
			return null;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 校验工作人员编号存在性
	 */
	public String getWorkerInfo()
	{
		try
		{
			WorkerEntity workerInfo = this.workerService.searchWorkerById(this.worker);
			if (workerInfo != null)
			{
				writeJson(workerInfo, false, false);
			}
			else
			{
				writeJson("");
			}
			return null;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}

	/**
	 * 设定工作人员.
	 * 
	 * @param worker 工作人员
	 */
	public void setWorker(WorkerEntity worker)
	{
		this.worker = worker;
	}

	/**
	 * 获取工作人员.
	 * 
	 * @return 工作人员
	 */
	public WorkerEntity getWorker()
	{
		return this.worker;
	}

	/**
	 * 设定工作人员service.
	 * 
	 * @param workerService 工作人员service
	 */
	public void setWorkerService(WorkerService workerService)
	{
		this.workerService = workerService;
	}

	/**
	 * 获取工作人员service.
	 * 
	 * @return 工作人员service
	 */
	public WorkerService getWorkerService()
	{
		return this.workerService;
	}

	/**
	 * 设定工作人员检索结果（分页）.
	 * 
	 * @param pagerEntity 工作人员检索结果（分页）
	 */
	public void setPagerEntity(PagerEntity pagerEntity)
	{
		this.pagerEntity = pagerEntity;
	}

	/**
	 * 获取工作人员检索结果（分页）.
	 * 
	 * @return 工作人员检索结果（分页）
	 */
	public PagerEntity getPagerEntity()
	{
		return this.pagerEntity;
	}

	/**
	 * 设定工作人员检索条件.
	 * 
	 * @param workerSearchEntity 工作人员检索条件
	 */
	public void setWorkerSearchEntity(WorkerSearchEntity workerSearchEntity)
	{
		this.workerSearchEntity = workerSearchEntity;
	}

	/**
	 * 获取工作人员检索条件.
	 * 
	 * @return 工作人员检索条件
	 */
	public WorkerSearchEntity getWorkerSearchEntity()
	{
		return this.workerSearchEntity;
	}

	/**
	 * 获取岗位列表.
	 * 
	 * @return 岗位列表
	 */
	public List<ComboBoxEntity> getJobList()
	{
		try
		{
			return this.comboBoxService.searchJobList();
		}
		catch (Exception e)
		{

		}

		return new ArrayList<ComboBoxEntity>();
	}

	/**
	 * 设定下拉框service.
	 * 
	 * @param comboBoxService 下拉框service
	 */
	public void setComboBoxService(ComboBoxService comboBoxService)
	{
		this.comboBoxService = comboBoxService;
	}

	/**
	 * 获取下拉框service.
	 * 
	 * @return 下拉框service
	 */
	public ComboBoxService getComboBoxService()
	{
		return this.comboBoxService;
	}

}
